package com.rogchen.security.securitydemo.service;


import com.rogchen.security.securitydemo.dto.SysPermission;
import com.rogchen.security.securitydemo.dto.SysRole;
import com.rogchen.security.securitydemo.dto.SysUser;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Rogchen rogchen128@gmail.com
 * @description:
 * @product: IntelliJ IDEA
 * @created Date: 19-11-4 09:46
 **/
@Service
public class UserService {
    public SysUser getUserByName(String username) {
        BCryptPasswordEncoder cy = new BCryptPasswordEncoder();
        String pwd = cy.encode("123456");
        SysRole admin = new SysRole("ROLE_ADMIN", "管理员");
        SysRole developer = new SysRole("ROLE_DEVELOPER", "开发者");
        SysPermission p1 = new SysPermission();
        p1.setCode("ROLE_ADMIN");    //角色对应的code
        p1.setName("个人中心");
        p1.setUrl("/index.html");
        SysPermission p2 = new SysPermission();
        p2.setCode("ROLE_ADMIN");
        p2.setName("图书列表");
        p2.setUrl("/data");
        SysPermission p3 = new SysPermission();
        p3.setCode("ROLE_DEVELOPER");
        p3.setName("添加图书");
        p3.setUrl("/book/add");
        SysPermission p4 = new SysPermission();
        p4.setCode("ROLE_DEVELOPER");
        p4.setName("查看图书");
        p4.setUrl("/book/detail");
        admin.setPermissionList(Arrays.asList(p1, p2, p3, p4));
        developer.setPermissionList(Arrays.asList(p1, p2));
        SysUser user = new SysUser();
        user.setUsername(username);
        user.setPassword(pwd);
        List<SysRole> roles = new ArrayList<>();
        roles.add(admin);
        user.setRoleList(roles);
        return user;
    }
}
